#ifndef SERIAL_WIDGET_H
#define SERIAL_WIDGET_H

#include <QWidget>
#include <QTextEdit>
#include <QLabel>
#include <QCheckBox>
#include <QLineEdit>
#include <QPushButton>
#include <QSpinBox>
#include <QComboBox>
#include <QGridLayout>
#include <QGroupBox>
#include <QString>
#include <QFile>
#include <QStringList>
#include <QList>
#include <QSerialport>
#include <QSerialPortInfo>
#include <QDebug>
#include <QTimer>
#include <QFileDialog>
#include <QStandardPaths>
#include <QDateTime>
#include <QDate>
#include <iostream>


#include "component/click_combobox.h"
#include "utils/utils.h"

class SerialWidget : public QWidget
{
    Q_OBJECT
public: 
    SerialWidget(QWidget *parent = nullptr);

private slots:
    void onOpenOrCloseSerialPort();
    void onSendContext();                                       // 发动按钮事件
    void onSerialReadToRead();                                  // 接收数据
    void on_tiemSendCheckBox_clicked(bool checked);             // 定时发送数据
    void on_clearReciveButton_clicked();                        // 清除接收区
    void on_saveReciveButton_clicked();                         // 保存接收区
    void on_refreshTime();                                      // 刷新时间
    void on_hexShowCheckBox_clicked(bool checked);              // 十六进制显示
    void on_hideControlPanelButton_clicked();                   // 隐藏控制面板
    void on_hideHistoryButton_clicked();                        // 隐藏历史记录
    void on_serialComboBox_clicked();                           // 串口选择
    void on_sendButtonItemClicked(int index);                   // 多文本发送按钮事件
    void on_loopSendTimer_timeout();                            // 循环发送线程
    void on_repeatCheckBox_clicked(bool checked);               // 重复发送
    void on_restButton_clicked();                               // 重置
    void on_saveButton_clicked();                               // 保存
    void on_loadButton_clicked();                               // 加载

private:
    void initUI();
    void loadStyleSheet();
    void initModel();
    void getSerialPortList();              // 获取串口列表
    void initBindingSignalSlot();          // 初始化信号与槽的绑定

private:
    QGridLayout *mainLayout;              // 主布局
    QGroupBox *receiveGroupBox;
    QGroupBox *historyGroupBox;
    QGroupBox *multiGroupBox;

    QTextEdit *receiveTextEdit;            // 接收区
    QTextEdit *historyTextEdit;            // 历史记录
    QCheckBox *hexCheckBox[13];
    QLineEdit *strLineEdit[13];
    QPushButton *sendButton[13];
    // 右上3下区域
    QCheckBox *repeatCheckBox;
    QSpinBox *repeatSpinBox;
    QPushButton *saveButton;
    QPushButton *loadButton;
    QPushButton *restButton;
    // 左下1区域 串口设置
    ClickComboBox *serialComboBox;
    QComboBox *baudRateComboBox;
    QComboBox *dataBitsComboBox;
    QComboBox *parityComboBox;
    QComboBox *stopBitsComboBox;
    QComboBox *flowControlComboBox;
    // 右下区域
    QPushButton *onToOffButton;
    QPushButton *clearReciveButton;
    QCheckBox *reciveTimeCheckBox;
    QCheckBox *hexShowCheckBox;
    QPushButton *runModeButton;
    QPushButton *hideControlPanelButton;
    QPushButton *saveReciveButton;
    QCheckBox *autoLineCheckBox;
    QPushButton *downloadModeButton;
    QPushButton *hideHistoryButton;

    QCheckBox *tiemSendCheckBox;
    QLineEdit *tiemSendLineEdit;
    QCheckBox *sendNewRowCheckBox;
    QCheckBox *hexSendCheckBox;
    QCheckBox *formatInCheckBox;

    QPushButton *sendMessageRowButton;
    QTextEdit *sendMessageEdit;

    QLabel *statusLabel;
    QLabel *receiveLabel;
    QLabel *sendStatusLabel;
    QLabel *timeLabel;


    QSerialPort *serialPort;        // 串口对象
    int readCount;
    int writeConunt;
    QString sendRecord;
    QTimer *timer;
    QTimer *refreshTime;
    bool showControlPanel = true;
    bool showHistory = true;
    QTimer *loopSendTimer;
    int currentIndex = 0;            // 当前发送项

};

#endif